package utils

import (
	"fmt"
	"time"
)

func IsAdult(idCard string) bool {
	birthDate, err := time.Parse("20060102", idCard[6:14])
	if err != nil {
		panic(err)
	}

	age := calculateAge(birthDate)
	fmt.Println(age)

	return age >= 18
}

func calculateAge(birthDate time.Time) int {
	now := time.Now()

	age := now.Year() - birthDate.Year()

	// 如果出生日期还没到今年的话，年龄减一
	if now.YearDay() < birthDate.YearDay() {
		age--
	}

	return age
}
